// pages/patient_pay/patient_pay.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timelist:["1","2","3","4","5","6","7","8","9","10"],
    index:0,
    read:false,
    zid:"",
    id:"",
    money:0.00,
    total:0.00,
    j_id:"",
    names:"",
    age:"",
    tizhong:"",
    sex:"",
    time:1,
    addresslist:[],
    xieyi: false,
    typs:"",

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var zid = options.zid;
    var that = this;
    var money = options.money;
    var id = options.id;
    that.setData({
      zid:zid,
      money:money,
      total:money,
      id:id,
    })
    if(options.typs != 'undefined' && options.typs != undefined)
    {
      that.setData({
        typs:options.typs
      })
    }
    that.getFuwu_detail()
    that.getZhuanjia();
    that.getUserinfo();
    that.getBase();
  },
  getBase:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.fuwu_base",
      success:function(res)
      {
        that.setData({
          base:res.data
        })
      }
    })
  },
  // 获取服务包详情
  getFuwu_detail:function(res)
  {
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.fuwu_detail",
      data:{
        id:that.data.id
      },
      success:function(res)
      {
        that.setData({
          fuwu_detail:res.data,
          equiment_money:res.data.equiment_money,
          
        })
        var total = 
        (parseFloat(res.data.equiment_money * 1) + 
        parseFloat(that.data.total * 1)).toFixed(2);
        that.setData({
          total:total
        })
      }
    })
  },
  // 获取专家详情
  getZhuanjia:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.zhuanjia_detail",
      data:{
        zid:that.data.zid
      },
      success:function(res)
      {
        that.setData({
          docinfo:res.data
        })
      }
    })
  },
  getUserinfo:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.userinfo",
      data:{
        j_id:that.data.j_id
      },
      success:function(res)
      {
        that.setData({
          userinfo:res.data
        })
      }
    })
  },
  // 切换就诊人
  bindpatient(e){
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=fuwu',
    })
  },
  // 服务时长
  bindPickerChange(e){
    var that = this;
    var timelist = that.data.timelist;
    var money = that.data.money;
    var time = timelist[e.detail.value];
    var total = (money * 1 * time * 1 + that.data.equiment_money * 1).toFixed(2);
    this.setData({
      index:e.detail.value,
      time:timelist[e.detail.value],
      total:total,
    })
  },
  // 勾选
  bindbtn(e){
    this.setData({
      read:!this.data.read
    })
  },
  // 协议
  xieyibtn: function () {
    this.setData({
      xieyi: true
    })
  },
  closezhe: function () {
    this.setData({
      xieyi: false,
      read:true,
    })
  },

  // 立即支付
  pay(e){
    var that = this;
    var j_id = that.data.j_id;
    var zid = that.data.zid;
    var money = that.data.money;
    var total = that.data.total;
    var addresslist = that.data.addresslist;
    if(addresslist.length == 0 || addresslist == '')
    {
      wx.showToast({
        title: '请选择收货地址',
      })
      return;
    }
    var time = that.data.time;
    if(j_id == '')
    {
      wx.showToast({
        title: '请选择就诊人',
      })
    }else if(time == '')
    {
      wx.showToast({
        title: '请选择服务时长',
      })
    }else if(that.data.read == false)
    {
      wx.showToast({
        title: '请先阅读协议',
      })
    }else{
      app.util.request({
        'url':"entry/wxapp/follow.add_fuwu",
        data:{
          j_id:j_id,
          zid:zid,
          time:time,
          total:total,
          money:money,
          f_id:that.data.id,
          name:that.data.addresslist.userName,
          phone:that.data.addresslist.userPhone,
          address:that.data.addresslist.userAddress,
          openid:wx.getStorageSync('openid')
        },
        success:function(res)
        {
          var ordersn = res.data.ordersn;
          if(money != 0 && money != '0.00' && money != '0' && money != '')
          {
            app.util.request({
              'url': 'entry/wxapp/follow.pay_fuwu',
              header: {
                "Content-Type": "application/xml"
              },
              method: "GET",
              data: {
                openid: wx.getStorageSync('openid'),
                money: total,
                orders:ordersn
              },
              success:function(res)
              {
                wx.requestPayment({
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr, //字符串随机数
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  success: function (res) {},
                  complete: function (res) {
                    if (res.errMsg != 'requestPayment:fail cancel') {
                      app.util.request({
                        'url':"entry/wxapp/follow.update_qianyue",
                        data:{
                          openid:wx.getStorageSync('openid'),
                          zid:zid
                        },
                      })
                      app.util.request({
                        'url': 'entry/wxapp/follow.update_fuwu',
                        data: {
                          openid: wx.getStorageSync('openid'),
                          back_orser: ordersn
                        },
                        success: function (payres) {
                          wx.showToast({
                            title: '支付成功!',
                          })
                          //提醒
                          app.util.request({
                            'url': 'entry/wxapp/follow.memberopentx',
                            data: {
                              near: '服务包购买',
                              ordersn:ordersn,
                              openid:wx.getStorageSync('openid'),
                              zid:that.data.zid
                            },
                            success: function (res) {
                              console.log(res)
                            }
                          })
                          setTimeout(function () {
                            wx.reLaunch({
                              url: '/hyb_yl/huanzhe/pages/fwb_order_detail/fwb_order_detail?back_orser='+ordersn+'&key_words=fuwu',
                            })
                          }, 1000)
                        },
                      })
                    }
                  }
                })
              }
            })
          }else{
            app.util.request({
              'url':"entry/wxapp/follow.update_qianyue",
              data:{
                openid:wx.getStorageSync('openid'),
                zid:zid
              },
            })
            app.util.request({
              'url': 'entry/wxapp/follow.update_fuwu',
              data: {
                openid: wx.getStorageSync('openid'),
                back_orser: ordersn
              },
              success: function (payres) {
                wx.showToast({
                  title: '支付成功!',
                })
                //提醒
                app.util.request({
                  'url': 'entry/wxapp/follow.memberopentx',
                  data: {
                    near: '服务包购买',
                    openid:wx.getStorageSync('openid'),
                    zid:that.data.zid
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/hyb_yl/huanzhe/pages/fwb_order_detail/fwb_order_detail?back_orser='+ordersn+'&key_words=fuwu',
                  })
                }, 1000)
              },
            })
          }
        }
      })
    }
    
    // wx.navigateTo({
    //   urelsel: '/pages/fwb_order_detail/fwb_order_detail',
    // })
  },
  address: function () {
    console.log(1)
    var region = [];
    var arr1 = [];
    var that = this
    wx.chooseAddress({
      success: function (res) {
        region.push(res.provinceName, res.cityName, res.countyName)
        var addlist = {
          name: res.userName,
          phone: res.telNumber,
          address: res.detailInfo,
          region: region,
          label: -1,
          addlabel: ""
        }
        //添加收货人信息
        var userName = addlist.name
        var userPhone = addlist.phone
        var userAddress = addlist.region[0] + '-' + addlist.region[1] + '-' + addlist.region[2] + '-' + addlist.address
        app.util.request({
          'url': 'entry/wxapp/goods.creatuseraddress',
          data: {
            openid: wx.getStorageSync('openid'),
            userName: userName,
            userPhone: userPhone,
            userAddress: userAddress
          },
          success: function (res) {
            console.log(res)
            var addressid = res.data.addressId
            wx.setStorageSync("Address", res.data)
            that.setData({
              addressid: addressid,
              addresslist: res.data
            })
            if (res.data == "") {
              that.setData({
                addbox: true
              })
            } else {
              that.setData({
                addbox: false
              })
            }
          }
        })
        arr1 = wx.getStorageSync("Address") || [];
        that.setData({
          addresslist: arr1
        })
      },
      fail: function (err) {
        that.openConfirm()


      }
    })
  },
  openConfirm: function () {
    wx.showModal({
      content: '检测到您没打开地址权限，是否去设置打开？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({
            success: (res) => {
              console.log(res)
            } //打开设置面板
          })
        } else {
          console.log('用户点击取消')
        }
      }
    });
  },
})